package com.bykj.taketurns.homefragment.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bykj.taketurns.R;
import com.bykj.taketurns.activity.BaseActivity;
import com.bykj.taketurns.activity.ReadWordActivity;
import com.bykj.taketurns.activity.WriteScreenActivity;
import com.bykj.taketurns.customview.ThumbnailView;
import com.bykj.taketurns.homefragment.adapter.StageTextGridAdapter;
import com.bykj.taketurns.homefragment.bean.StageTextBean;
import com.bykj.taketurns.utils.Constant;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/8/8.
 */

public class StageTextActivity extends BaseActivity {

    private TextView stage_tv_title;
    private TextView stage_tv_script_detail;
    private TextView stage_tv_script_all;
    private RelativeLayout stage_rela_script_detail;
    private RelativeLayout stage_rela_script_all;
    private GridView stage_recy;
    private StageTextBean stageTextBean;
    private StageTextGridAdapter stageTextGridAdapter;


    @Override
    public void onResume() {
        super.onResume();
        postData();
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        ThumbnailView stage_thv_back = findViewById(R.id.stage_thv_back);
        ThumbnailView stage_thv_share = findViewById(R.id.stage_thv_share);
        stage_tv_title = findViewById(R.id.stage_tv_title);
        stage_tv_script_detail = findViewById(R.id.stage_tv_script_detail);
        stage_tv_script_all = findViewById(R.id.stage_tv_script_all);
        ImageView stage_img_pulldown = findViewById(R.id.stage_img_pulldown);
        ImageView stage_img_pullup = findViewById(R.id.stage_img_pullup);
        stage_rela_script_detail = findViewById(R.id.stage_rela_script_detail);
        stage_rela_script_all = findViewById(R.id.stage_rela_script_all);
        stage_recy = findViewById(R.id.stage_recy);

        findViewById(R.id.iv_text_record).setOnClickListener(this);
        stage_thv_back.setOnClickListener(this);
        stage_thv_share.setOnClickListener(this);
        stage_img_pulldown.setOnClickListener(this);
        stage_img_pullup.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        stage_recy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(StageTextActivity.this,ReadWordActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public int getParentView() {
        return R.layout.activity_stage_text;
    }

    @Override
    public void initToolBar() {

    }



    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.stage_thv_back:
                finish();
                break;
            case R.id.stage_thv_share:
                break;
            case R.id.stage_img_pulldown:
                stage_rela_script_detail.setVisibility(View.GONE);
                stage_rela_script_all.setVisibility(View.VISIBLE);
                break;
            case R.id.stage_img_pullup:
                stage_rela_script_detail.setVisibility(View.VISIBLE);
                stage_rela_script_all.setVisibility(View.GONE);
                break;
            case R.id.iv_text_record://跳转剧本编辑
                startActivity(null, WriteScreenActivity.class);
                break;
        }
    }

    public void postData() {

        Intent intent = getIntent();
        String tid = intent.getStringExtra("tid");
        String pid = intent.getStringExtra("pid");

        stage_tv_title.setText(intent.getStringExtra("title"));
        this.map.put("tid", tid);
        this.map.put("pid", pid);

        OkGo.get(Constant.JHC_SERVICE + Constant.ServiceConstant.TEXT_SECONDARY_PAGE + Constant.SetUrl(this.map))     // 请求方式和请求url
                .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
                .execute(new StringCallback() {



                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        stageTextBean = gson.fromJson(s, StageTextBean.class);

                        List<StageTextBean.CharacterListAllBean> characterListAll = stageTextBean.getCharacterListAll();

                        stageTextGridAdapter = new StageTextGridAdapter(StageTextActivity.this,characterListAll);

                        stage_recy.setAdapter(stageTextGridAdapter);

                    }

                    @Override
                    public void onCacheSuccess(String s, Call call) {


                    }
                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        map.remove("tid");
        map.remove("pid");
        map=null;
        OkGo.getInstance().cancelTag(this);
        Glide.get(this).clearMemory();
        stageTextGridAdapter=null;
    }
}
